package cn.stronglink.assetmanage.business.inspection.presenter;

import android.content.Context;
import android.os.Handler;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inspection.model.PatrolModel;
import cn.stronglink.assetmanage.business.inspection.view.PatrolView;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.dao.SyncTypesDao;
import cn.stronglink.assetmanage.entity.Patrol;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.SyncTypes;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.netrequest.business.patrol.PatrolService;
import cn.stronglink.netrequest.business.search.SearchService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by zhengjinglan on 2018/6/20.
 */

public class PatrolPresenter {

    private PatrolView patrolView;
    private PatrolModel patrolModel;
    private Context mContext;
    private SearchService searchService;
    private Handler mHandler;
    private Gson gson;

    public PatrolPresenter(Context context,PatrolView patrolView, PatrolModel patrolModel){
        this.patrolView = patrolView;
        this.patrolModel = patrolModel;
        this.mContext = context;
        searchService = new SearchService();
        mHandler = new Handler();
        gson = new Gson();
    }




    // 查询资产列表
    private void queryAssetListFromWeb(){
        try {

            searchService.downloadAssetData(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            patrolView.hideProgressDialog();
                            patrolView.showToast(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    Result<List<SearchAsset>> result = null;
                    result = gson.fromJson(json,new TypeToken<Result<List<SearchAsset>>>(){}.getType());
                    if (result!=null&&result.getHttpCode()== HttpURLConnection.HTTP_OK){
                        final List<SearchAsset> searchAssetList = result.getData();
                        if (searchAssetList!=null&&searchAssetList.size()>0){
                            //保存数据到本地
                            saveDataToLocal(searchAssetList);


                        }
                    }
                    patrolView.hideProgressDialog();

                }
            });
        } catch (final Exception e) {
            e.printStackTrace();
            patrolView.hideProgressDialog();
        }
    }


    // 保存资产数据到本地
    private void saveDataToLocal(List<SearchAsset> searchAssetList){
        if (searchAssetList!=null){
            DaoManager.getDaoSession(mContext).getSearchAssetDao().deleteAll();
            DaoManager.getDaoSession(mContext).getSearchAssetDao().insertInTx(searchAssetList);
        }
    }
}
